//
//  GWEmotionListView.m
//  新浪微博
//
//  Created by qianfeng on 16/2/20.
//  Copyright (c) 2016年 高炜. All rights reserved.
//

#import "GWEmotionListView.h"
#import "GWEmotionGrideView.h"

@interface GWEmotionListView ()<UIScrollViewDelegate>
@property (nonatomic,weak)UIScrollView *emotionList;
@property (nonatomic,weak)UIPageControl *pageControl;
@end
@implementation GWEmotionListView

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self =[super initWithFrame:frame]) {
        
        //1.表情列表
        UIScrollView *emotionList =[[UIScrollView alloc]init];
        emotionList.showsHorizontalScrollIndicator =NO;
        emotionList.showsVerticalScrollIndicator =NO;
        emotionList.pagingEnabled =YES;
        [self addSubview:emotionList];
        self.emotionList =emotionList;
        emotionList.delegate =self;
        
        //2.pageControl
        UIPageControl *pageControl =[[UIPageControl alloc]init];
        pageControl.hidesForSinglePage =YES;
        [pageControl setValue:[UIImage imageNamed:@"compose_keyboard_dot_selected"] forKeyPath:@"_currentPageImage"];
        [pageControl setValue:[UIImage imageNamed:@"compose_keyboard_dot_normal"] forKeyPath:@"_pageImage"];
        [self addSubview:pageControl];
        self.pageControl =pageControl;
        
    }
    return self;
}
-(void)setEmotionSource:(NSArray *)emotionSource
{
    
    _emotionSource =emotionSource;
    self.pageControl.currentPage =0;
    int count =(int)(emotionSource.count +GWEmotionMaxCountPrePage -1)/GWEmotionMaxCountPrePage;
    self.pageControl.numberOfPages =count;
    int currentCount = (int)self.emotionList.subviews.count;
    
//    [self.emotionList.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    for (int i=0; i<count; i++) {
        GWEmotionGrideView *grideView =nil;
        if (i>=currentCount) {//emotionList的子控件不足以复用
           grideView = [[GWEmotionGrideView alloc]init];
            [self.emotionList addSubview:grideView];
        }
        else
        {
            grideView =self.emotionList.subviews[i];
        }
        
        int loc =i*20;
        int len =20;
        if (loc+len>emotionSource.count) {
            len =(int)emotionSource.count-loc;
        }
        NSRange range =NSMakeRange(loc, len);
        grideView.emotions =[emotionSource subarrayWithRange:range];
        grideView.hidden =NO;
    }
    
    for (int i =count; i<currentCount; i++) {
        GWEmotionGrideView *grideView =self.emotionList.subviews[i];
        grideView.hidden =YES;
    }
    
    [self setNeedsLayout];
    self.emotionList.contentOffset =CGPointZero;
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    
    self.pageControl.width =GWScreenW;
    self.pageControl.height =35;
    self.pageControl.y =self.height-self.pageControl.height;
    
    self.emotionList.width =GWScreenW;
    self.emotionList.height =self.pageControl.y;
    int count =(int)(self.emotionSource.count +GWEmotionMaxCountPrePage -1)/GWEmotionMaxCountPrePage;;
    CGFloat grideW =self.width;
    CGFloat grideH =self.emotionList.height;
    self.emotionList.contentSize =CGSizeMake(grideW*count, 0);
    for (int i=0; i<count; i++) {
        UIView *grideView = self.emotionList.subviews[i];
        grideView.width =grideW;
        grideView.height =grideH;
        grideView.x =i*grideW;
    }
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    self.pageControl.currentPage =self.emotionList.contentOffset.x/self.width+0.5;
}
@end
